package cn.usian.product.service.impl;

import java.io.*;
import java.net.URLDecoder;
import java.util.*;

public class RandomStu {
    private static ArrayList<String> list = new ArrayList<>();

    static {
        try {
            //FileReader fr = new FileReader("stu.txt");
            InputStream fr = RandomStu.class.getResourceAsStream("stu.txt");
            BufferedReader bf = new BufferedReader(new InputStreamReader(fr, "gbk"));
            while (true) {
                String s = bf.readLine();
                if (s != null && s.trim().length() > 0) {
                    list.add(s);
                } else {
                    break;
                }
            }
            Collections.shuffle(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        String path = RandomStu.class.getClassLoader().getResource("").getPath();
        String decode = URLDecoder.decode(path, "utf-8");
        FileOutputStream outputStream = new FileOutputStream(new File(decode, "Hit.txt"), true);
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
        bufferedWriter.write(new Date().toLocaleString());
        bufferedWriter.newLine();
        bufferedWriter.flush();
        while (true) {
            System.out.println("请选择模式:\n1:逐个选;\n2:批量选;\n3:退出系统!");
            int i1 = sc.nextInt();
            switch (i1) {
                case 1:
                    selectOne(bufferedWriter);
                    break;
                case 2:
                    System.out.println("请输入选择的人数:");
                    int i = sc.nextInt();
                    for (int j = 1; j <=i ; j++) {
                        if (list.size() == 0) {
                            System.out.println("所有学生已经全部点名一遍了；");
                            break;
                        }
                        String remove = list.remove(new Random().nextInt(list.size()));
                        bufferedWriter.write(remove);
                        bufferedWriter.newLine();
                        bufferedWriter.flush();
                        System.out.println("批量选中的第" + j + "位幸运学生是：" + remove);
                    }
                    break;
                case 3:
                    System.out.println("拜拜!");
                    bufferedWriter.write("-------------------------------");
                    bufferedWriter.close();
                    return;
                default:
                    System.out.println("输入有误!");
            }
        }
    }

    private static void selectOne(BufferedWriter bufferedWriter) throws IOException {
        String remove1 = list.remove(0);
        System.out.println("今日第1位幸运学生是：" + remove1);
        bufferedWriter.write(remove1);
        bufferedWriter.newLine();
        bufferedWriter.flush();
        Scanner sc = new Scanner(System.in);
        int i = 2;
        while (true) {
            System.out.println("回车查看下一位同学(输入0返回上级菜单):");
            String s = sc.nextLine();
            if("0".equals(s)){
                return;
            }
            if (list.size() == 0) {
                System.out.println("所有学生已经全部点名一遍了；");
                break;
            }
            String remove = list.remove(new Random().nextInt(list.size()));
            bufferedWriter.write(remove);
            bufferedWriter.newLine();
            bufferedWriter.flush();
            System.out.println("今日第" + i++ + "位幸运学生是：" + remove);
        }
    }
}